package com.optimize.elasticsearch.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;


@Slf4j
@Configuration
public class EsClientConfig {

    @Bean
    public RestHighLevelClient elasticsearchClient() {

        return new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("127.0.0.1", 9200, "http"),
                        new HttpHost("127.0.0.1", 9201, "http"),
                        new HttpHost("127.0.0.1", 9202, "http")
                )
        );
    }

    @Bean
    public void close() {
        try {
            elasticsearchClient().close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
